String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
log = function(message) { if(console) { console.log(message);}};

var rmh = function(){};
rmh.init = function() {   
    // TODO: get this icon to now wrap.
    //$.datepicker.setDefaults({showOn: 'both',buttonImageOnly: true,buttonImage: '/Content/images/calendar.gif'});         
    $(".date").datepicker({ duration: 'fast' });
};

rmh.showMessage =  function(data) {
        if (!rmh.messageCount)
            rmh.messageCount = 0;
        rmh.messageCount++;
        messageID = "filterArray" + rmh.messageCount;
        newMessage = $('<div class="message ui-state-highlight" id="' + messageID + '" name="' + messageID + '">' + data + "</div>");
        $('#innerMessage').append(newMessage);
        setTimeout("$('#" + messageID + "').fadeOut('fast');", 3900);
    };


rmh.ajax = {
    ajaxRegion: function() { return $('#ajaxSpan') },
    start: function() {
        this.ajaxRegion().show(1);
    },
    end: function() {
        setTimeout(function() {
            rmh.ajax.ajaxRegion().fadeOut('slow');            
        }, 40);
    },
    run: function(callback) {
        rmh.ajax.start();
        setTimeout(function(){
            callback();
            rmh.ajax.end();
            },200);
        return false;
    },
    validate: function(conditional, callback) {
        if (conditional) return conditional;
        eval(callback)
        rmh.ajax.end();
        return false;
    }
}

rmh.searchSupportPeople = function() {
    url = "/SupportPerson/DropDown?"
    firstName = $('#supportPersonFirstName').val().trim();
    lastName = $('#supportPersonLastName').val().trim();
    
    if (firstName) url += "firstName=" + firstName + "&"
    if (lastName) url += "lastName=" + lastName + "&"
    $("#supportPersonContainer").load(url);           
};

// todo: not dry with adding diagnosis, jerkwad
// i'm going to take your clipboard and beat your 
// face with it.
rmh.addSupportPerson = function(id, name, relationshipId, relationshipName) {
    
    // TODO add edit link?
    
    if($('input.sp[value=' + id + ']').length) {
        rmh.showMessage("Support Person already exists.");
        return;
    }
    var div = $("#relationshipList");
    var htmlToAppend = '<div class="listItem"><input class="sp" name="SupportPerson"' + 
        ' type="hidden" value="' + id + '"/>';
    htmlToAppend += '<input class="rs" name="RelationShipType"' + 
        ' type="hidden" value="' + relationshipId + '"/>';
    htmlToAppend += name + " - " + relationshipName
    htmlToAppend += ' <span class="removeParentDiv">(remove)</span></div>';
    
    div.append(htmlToAppend);    
};

rmh.addSelectedSupportPerson = function() {
    // aSLKDJASLKDJASLDKJ
    var selectedOption = $('#SupportPersonID :option[selected]');
    var selectedName = selectedOption.attr("personName");
    var selectedId = selectedOption.val();
    var selectedRelationship = $('#RelationshipType :option[selected]');
    var selectedRelationshipId = selectedRelationship.val();
    var selectedRelationshipName = selectedRelationship.text();
    rmh.addSupportPerson(selectedId, selectedName,
        selectedRelationshipId, selectedRelationshipName);
};

$(function() {rmh.init();});